import { Message } from 'element-ui';
const regExp = {
  /**限制非中文符号*/
  codeReg(data) {
    let temp = ""
    for (var i = 0; i < data.length; i++) {
      if (data.charCodeAt(i) > 0 && data.charCodeAt(i) < 255) {
        temp += data.charAt(i)
      }
    }
    return temp
  },
  /**验证座机和手机*/
  landlinePhoneReg(data) {
    //座机
    var reg1 = new RegExp(/^([0-9]{3,4}-)?[0-9]{7,8}$/);
    //手机
    var reg = new RegExp(/^[1-9]\d{10}$/);
    if (reg.test(data) || reg1.test(data)) {
      return true
    }
    return false
  },
  /**身份证验证*/
  cardReg(data) {
    let reg = /^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\d{4}(([1][9]\d{2})|([2]\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\d{3}[0-9xX]$/
    let re = new RegExp(reg);
    return re.test(data)
  },
  /**手机号验证*/
  phoneReg(data) {
    let reg = /^1[3456789]\d{9}$/;
    let re = new RegExp(reg);
    return re.test(data)
  },
  /**验证两位数如：10,20*/
  sortReg(data) {
    let reg = /^[+]{0,1}(\d+)$/;
    let re = new RegExp(reg);
    return re.test(data)
  },
  /**所有车牌号验证*/
  carReg(data) {
    let reg = /(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$)/;
    let re = new RegExp(reg);
    return re.test(data)
  },
  /**实时验证数字含小数,小数最多2位，不能超过12位数*/
  checkNum(e) {
    if (e == '00') {
      return 0
    }
    var value = e.replace(/\./ig, function (a, b, c) {
      return c.indexOf(a) == b ? '.' : '';
    });
    if (value.toString().split(".").length > 1 && value.toString().split(".")[1].length > 2) {
      value = value.slice(0, value.length - 1)
    }
    if (value > 999999999999) {
      value = 999999999999
    }
    return value
  },
  /**实时验证数字含小数,小数最多3位*/
  checkNum3(e) {
    var value = e.replace(/\./ig, function (a, b, c) {
      return c.indexOf(a) == b ? '.' : '';
    });
    if (value.toString().split(".").length > 1 && value.toString().split(".")[1].length > 3) {
      value = value.slice(0, value.length - 1)
    }
    if (value > 999999999999) {
      value = 999999999999
    }
    return value
  },
  /**离开时验证数据格式，和正整数是否是00开头或者全是0*/
  blurCheckNum(e, negativeType) {
    if (!e) {
      return 0
    }
    if (negativeType) {//是否判断负数格式！
      var regu = new RegExp(/^([-+])?\d+(\.[0-9]{1,2})?$/)
      if (regu.test(e)) {
        return e
      }
    }
    let re = new RegExp(/^(0|[1-9]\d*)(\.\d{0,3})?$/);
    if (!re.test(e)) {
      Message({
        message: '请输入正确数值！',
        type: 'warning'
      });
      return 0
    }
    let re0 = new RegExp(/^[0]*$/);
    if (e.toString().split(".").length > 1 && (re0.test(e.toString().split(".")[0]) && re0.test(e.toString().split(".")[1]))) {
      Message({
        message: '请输入正确数值！',
        type: 'warning'
      });
      return 0
    }
    return e
  },
  FloatMul(arg1, arg2) {//乘法
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    // eslint-disable-next-line no-empty
    try { m += s1.split(".")[1].length } catch (e) { }
    // eslint-disable-next-line no-empty
    try { m += s2.split(".")[1].length } catch (e) { }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  }
}
export default regExp;
